﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Abp.Domain.Repositories;
using Abp.ElasticSearch;
using AbpBuild;
using AbpBuild.LawManage;
using AbpBuild.WenShu;
using AbpBuild.WenShu.Dto;
using Newtonsoft.Json.Linq;

using System;
using DotnetSpider.Selector;
using System.Collections;
using Nest;
using System.Text.RegularExpressions;
using Abp.EntityFrameworkCore.Repositories;
using Microsoft.EntityFrameworkCore;
using Castle.DynamicProxy.Generators.Emitters.SimpleAST;
using Abp.Json;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Newtonsoft.Json;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Abp.Domain.Uow;
using System.Security.Policy;
using System.Web;
using Microsoft.AspNetCore.Mvc;
using System.Reflection;
using AbpBuild.BaseData;
using Castle.Core.Internal;

using System.Net;
using CsharpHttpHelper;
using System.Threading;
using AutoMapper;
using Microsoft.Extensions.Configuration;
using AbpBuild.Configuration;
using Abp.Application.Services;

namespace AbpBuild.WenShu
{
    /// <summary>
    /// 爬取公司简介
    /// </summary>
    public class CompanySearshAppService : AbpBuildAppServiceBase, ICompanySearshAppService
    {
        //private readonly IRepository<companyRela, string> _companyRela;
        private readonly IRepository<company, string> _company;
        private static IConfigurationRoot _appConfiguration = AppConfigurations.Get(System.Environment.CurrentDirectory);
        //14         //用法1(有嵌套)：GetAppSetting("Authentication", "JwtBearer:SecurityKey")
        //15         //用法2：GetAppSetting("App", "ServerRootAddress")
        //16         public static string GetAppSetting(string section, string key)
        //17         {
        //18 
        //19             return _appConfiguration.GetSection(section)[key];
        //20         }
        //21 
        //22         public static string GetConnectionString(string key)
        //23         {
        //24             return _appConfiguration.GetConnectionString(key);
        //25         }
        readonly IUnitOfWorkManager _unitOfWorkManager;


        public CompanySearshAppService(
            //IRepository<companyRela, string> companyRelarepository,
            IRepository<company, string> companyrepository,
            IUnitOfWorkManager unitOfWorkManager
        )
        {
            //_companyRela = companyRelarepository;
            _company = companyrepository;
            _unitOfWorkManager = unitOfWorkManager;
        }

        /// <summary>
        /// 爱企查
        /// </summary>
        /// <param name="companyname"></param>
        /// <returns></returns>
        [UnitOfWork(System.Transactions.IsolationLevel.ReadUncommitted)]
        [RemoteService(false)]
        public companyJM GetcompanyAQC(string companyname, string docid)
        {
            if (companyname.Length > 35)
            {
                return new companyJM();
            }
            companyJM companyJM = new companyJM();
            company company = _company.GetAll().Where(p => p.companyname == companyname).FirstOrDefault();
            if (company != null)
            {
                return ObjectMapper.Map<companyJM>(company);
            }
            else
            {
                company = new company();
                company.companyname = companyname;
                _company.Insert(company);
                CurrentUnitOfWork.SaveChanges();
                return new companyJM();
            }

        }



        [UnitOfWork(System.Transactions.IsolationLevel.ReadUncommitted)]
        [RemoteService(false)]
        public void GetcompanyAQCConsole()
        {
            bool isIP = Convert.ToBoolean(_appConfiguration.GetConnectionString("IP"));
            int taltal = 0;
            int srows = 0;
            int srowtotl = 0;
            int indexed = 0;
            while (true)
            {
                try
                {

                    Console.WriteLine("开始");

                    using (var uowHandle = _unitOfWorkManager.Begin())
                    {
                        if (taltal == 0)
                        {
                            //taltal = _companyRela.GetAll().Where(p => p.Indexed == 0 || p.Indexed == null).Count();
                        }

                        List<company> relas = _company.GetAll().Where(p => string.IsNullOrEmpty(p.companyfullname)).Take(5).ToList();

                        srowtotl += relas.Count;

                        foreach (var rela in relas)
                        {
                            if (!isIP)
                            {
                                Thread.Sleep(2000);
                            }
                            srows += 1;
                            Console.WriteLine("总数：" + taltal.ToString() + ",当前第" + srows.ToString() + " /" + srowtotl.ToString() + ",剩下:" + (taltal - srows).ToString() + "，indexed:" + indexed + "," + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));

                            //河北君太非融资性担保有限公司（原河北盛唐鸿明担保有限公司）
                            //rela.alia = "中国农业银行股份有限公司";
                            if (rela.companyname.IndexOf("？") >= 0 || rela.companyname.IndexOf("?") >= 0 || rela.companyname.IndexOf("与") >= 0 || rela.companyname.IndexOf("·") >= 0 || rela.companyname.IndexOf("某") >= 0 || rela.companyname.IndexOf("等") >= 0 || rela.companyname.IndexOf("×") >= 0 || rela.companyname.IndexOf("＊") >= 0 || rela.companyname.IndexOf("、") >= 0 || rela.companyname.IndexOf("，") >= 0 || rela.companyname.IndexOf("。") >= 0)
                            {
                                rela.companyfullname = "无";
                                _company.Update(rela);
                                Console.WriteLine("无：" + rela.companyname + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
                                continue;
                            }
                            string tttmep = rela.companyname.Replace("(", "（").Replace(")", "）");
                            MatchCollection mcttt = Regex.Matches(tttmep, "（.{1,25}?）");  //有问题
                            if (mcttt.Count == 1 && tttmep.Substring(tttmep.Length - 1, 1) == "）")
                            {
                                tttmep = tttmep.Replace(mcttt[0].Groups[0].Value, "");
                            }
                            if (mcttt.Count > 1)
                            {
                                tttmep = tttmep.Replace(mcttt[mcttt.Count - 1].Groups[0].Value, "");
                            }


                            if (tttmep.IndexOf("分公司") > 0)
                            {
                                tttmep = tttmep.Substring(0, tttmep.IndexOf("分公司") + 3);
                            }
                            else if (tttmep.IndexOf("分行") > 0)
                            {
                                tttmep = tttmep.Substring(0, tttmep.IndexOf("分行") + 2);
                            }
                            else if (tttmep.IndexOf("支公司") > 0)
                            {
                                tttmep = tttmep.Substring(0, tttmep.IndexOf("支公司") + 3);
                            }
                            else if (tttmep.IndexOf("支行") > 0)
                            {
                                tttmep = tttmep.Substring(0, tttmep.IndexOf("支行") + 2);
                            }
                            else if (tttmep.IndexOf("公司") > 0)
                            {
                                tttmep = tttmep.Substring(0, tttmep.IndexOf("公司") + 2);
                            }
                            bool isok = false;
                            Console.WriteLine("爬取开始[" + tttmep + "]," + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));

                            while (!isok)
                            {
                                try
                                {

                                    string value = HttpUtility.UrlEncode(tttmep, Encoding.UTF8).Replace("+", "%20");
                                    //创建Httphelper对象
                                    HttpHelper http = new HttpHelper();
                                    //创建Httphelper参数对象

                                    WebProxy webProxy = new WebProxy();
                                    webProxy.Address = new Uri(string.Format("{0}:{1}", "http://tps163.kdlapi.com", "15818"));
                                    webProxy.Credentials = new NetworkCredential("t10853562674897", "4j83ks88"); //用户名：t19186421608282  密码：7a85i5lf

                                    HttpItem item = new HttpItem()
                                    {
                                        URL = $"https://aiqicha.baidu.com/s?q={value}&t=0",//URL     必需项    
                                        Method = "GET",//URL     可选项 默认为Get   
                                        Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
                                        Cookie = "BIDUPSID=C272367E3D85F5ECDC4FCB4BD9F6BCF9; PSTM=1598611814; ZX_UNIQ_UID=722a76031a1ef614585bde0da76b80c4; Hm_lvt_baca6fe3dceaf818f5f835b0ae97e4cc=1599791699,1599792332; ZX_HISTORY=%5B%7B%22visittime%22%3A%222020-09-11+10%3A48%3A38%22%2C%22pid%22%3A%22xlTM-TogKuTwZbKDp9AU7PhY0n9kUH7M1Amd%22%7D%5D; Hm_lpvt_baca6fe3dceaf818f5f835b0ae97e4cc=1599792520; BDRCVFR[tFA6N9pQGI3]=mk3SLVN4HKm; delPer=0; PSINO=6; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; ZD_ENTRY=baidu; BAIDUID=1B1B9DA3514D7EC08D4B0F2E3AD9C0F0:FG=1; H_PS_PSSID=32699",
                                        Host = "aiqicha.baidu.com",
                                        Referer = $"https://aiqicha.baidu.com/s?q={value}&t=0",
                                        UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36",
                                        //PostDataType = PostDataType.String,//默认为字符串，同时支持Byte和文件方法
                                        //PostEncoding = System.Text.Encoding.UTF8,//默认为Default，
                                        //Postdata = "__VIEWSTATE=%2FwEPDwUKLTQ3MzE1NzE2MWQYAQUeX19Db250cm9sc1JlcXVpcmVQb3N0QmFja0tleV9fFgIFCmlzUmVtZW1iZXIFC2lzQXV0b0xvZ2luBX5spPlbkbdaOOfsiLY2g0mJe7M%3D&__VIEWSTATEGENERATOR=C2EE9ABB&WebUCHead_Special1%24hiddIsLogin=0&WebUCSearchNoAdvSearch1%24lib=&keyword=&user_name=" + user + "&user_password=" + password,//Post要发送的数据
                                        //ContentType = "application/x-www-form-urlencoded",
                                        SecurityProtocol = SecurityProtocolType.Tls,
                                        WebProxy = isIP ? webProxy : null
                                    };

                                    item.Header.Add("Accept-Encoding", "gzip, deflate, br");
                                    item.Header.Add("Accept-Language", "zh-CN, zh; q=0.9");
                                    item.Header.Add("Connection", "keep-alive");
                                    item.Header.Add("sec-fetch-mode", "navigate");
                                    item.Header.Add("sec-fetch-site", "same-origin");
                                    item.Header.Add("sec-fetch-user", "?1");
                                    item.Header.Add("Origin", "https://aiqicha.baidu.com");
                                    item.Header.Add("Upgrade-Insecure-Requests", "1");

                                    HttpResult result = http.GetHtml(item);
                                    if (!string.IsNullOrEmpty(result.RedirectUrl) || result.StatusCode != HttpStatusCode.OK)
                                    {
                                        Console.WriteLine(" 频繁，暂停5");
                                        Thread.Sleep(5000);
                                    }
                                    string content = result.Html.Replace(" ", "");
                                    string[] sc = content.Split("</script>");

                                    for (int i = 0; i < sc.Length; i++)
                                    {
                                        if (sc[i].IndexOf("window.pageData") >= 0)
                                        {

                                            //string href = "https://aiqicha.baidu.com/detail/compinfo?pid=xlTM-TogKuTwpZhM*DDv0PHnObnM4KYl8wmd";
                                            string href = "https://aiqicha.baidu.com/detail/compinfo?pid=";
                                            string pid = string.Empty;
                                            sc[i] = sc[i].Replace("};\\", "}；\\").Replace("};\"", "}；\"").Replace("};'", "}；'").Replace("};:", "}；:").Replace("};u", "}；u").Replace("};{", "}；{").Replace("};}", "}；}").Replace("};,", "}；,");
                                            string pageData = sc[i].Substring(sc[i].IndexOf("window.pageData=") + 16, sc[i].IndexOf("};") - sc[i].IndexOf("window.pageData=") - 15);
                                            JObject jobj = JObject.Parse(pageData);
                                            if (jobj["result"] != null)
                                            {
                                                if (Convert.ToInt32(jobj["result"]["totalNumFound"]) == 0)
                                                {
                                                    rela.companyfullname = "无";
                                                    _company.Update(rela);
                                                    Console.WriteLine("未找到[" + tttmep + "]," + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
                                                    isok = true;
                                                }
                                            }
                                            else
                                            {
                                                Console.WriteLine("可能被屏蔽了，1请查看:" + sc[i]);
                                                Thread.Sleep(1000);
                                            }

                                            int pidnun = 0;
                                            int tmtmtm = 0;
                                            List<string> pids = new List<string>();
                                            JArray jarrayList = (JArray)jobj["result"]["resultList"];
                                            if (jarrayList.Count() > 0)
                                            {

                                                foreach (var jals in jarrayList)
                                                {
                                                    pids.Add(jals["pid"].ToString());
                                                    string sstemp = jals["entName"].ToString();

                                                    MatchCollection mct = Regex.Matches(sstemp, "<.{1,7}?>");  //有问题
                                                    for (int j = 0; j < mct.Count; j++)
                                                    {
                                                        sstemp = sstemp.Replace(mct[j].Groups[0].Value, "");
                                                    }
                                                    if (sstemp == tttmep)
                                                    {
                                                        pid = jals["pid"].ToString();
                                                        pidnun = tmtmtm;
                                                        break;
                                                    }
                                                    else if (sstemp.Contains(tttmep))
                                                    {
                                                        pid = jals["pid"].ToString();
                                                        pidnun = tmtmtm;
                                                        break;
                                                    }
                                                    tmtmtm = tmtmtm + 1;
                                                }
                                                if (string.IsNullOrEmpty(pid))
                                                {
                                                    pid = jarrayList[0]["pid"].ToString();
                                                    pidnun = 0;
                                                }
                                                pids.Remove(pid);
                                            }
                                            if (!string.IsNullOrEmpty(pid))
                                            {
                                                int trynum = 0;

                                            kap:
                                                item.URL = href + pid;


                                                string RedirectUrl = string.Empty;
                                                while (string.IsNullOrEmpty(RedirectUrl))
                                                {
                                                    HttpResult result2 = http.GetHtml(item);
                                                    RedirectUrl = result2.RedirectUrl;
                                                    if (string.IsNullOrEmpty(RedirectUrl))
                                                    {
                                                        Console.WriteLine("可能被屏蔽了，2请查看,返回连接为空");
                                                        Thread.Sleep(1500);
                                                    }
                                                }

                                                if (!string.IsNullOrEmpty(RedirectUrl) && !RedirectUrl.Contains("forbidden") && !RedirectUrl.Contains("?"))
                                                {
                                                    //https://aiqicha.baidu.com/company_detail_31610236813812
                                                    string newpid = RedirectUrl.Substring(RedirectUrl.IndexOf("detail_") + 7);
                                                    //https://aiqicha.baidu.com/detail/basicAllDataAjax?pid=31610236813812
                                                    string newurl = "https://aiqicha.baidu.com/detail/basicAllDataAjax?pid=" + newpid;
                                                    while (!isok)
                                                    {
                                                        item.URL = newurl;
                                                        HttpResult result3 = http.GetHtml(item);
                                                        if (!string.IsNullOrEmpty(result3.RedirectUrl) || result3.StatusCode != HttpStatusCode.OK)
                                                        {
                                                            Console.WriteLine(" 频繁，暂停5");
                                                            Thread.Sleep(5000);
                                                        }
                                                        string html = result3.Html;
                                                        
                                                        try
                                                        {
                                                            JObject jobjD = JObject.Parse(html);
                                                            if (jobjD["status"].ToString() == "2" && jobjD["msg"].ToString() == "系统异常，请联系管理员")
                                                            {
                                                                trynum += 1;
                                                            }
                                                            if (trynum >3)
                                                            {
                                                              
                                                                rela.companyfullname = "无";
                                                                _company.Update(rela);
                                                                Console.WriteLine("未找到[" + tttmep + "]," + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff") + ",trynum：" + trynum);
                                                                isok = true;
                                                                break;

                                                            }
                                                            if (jobjD["data"].ToString() == "")
                                                            {
                                                                if (pids.Count == 0)
                                                                {
                                                                    rela.companyfullname = "无";
                                                                    _company.Update(rela);
                                                                    Console.WriteLine("未找到[" + tttmep + "]," + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
                                                                    isok = true;
                                                                }
                                                                else
                                                                {
                                                                    pid = pids[0];
                                                                    goto kap;
                                                                }
                                                            }
                                                            else
                                                            {
                                                                if (jobjD["data"]["basicData"] != null)
                                                                {
                                                                    rela.companyfullname = jobjD["data"]["basicData"]["entName"].ToString();
                                                                    rela.mainPeople = jobjD["data"]["basicData"]["legalPerson"].ToString().Replace("*", "");   //法定代表人 
                                                                    rela.address = jobjD["data"]["basicData"]["regAddr"].ToString();  //企业住所
                                                                    rela.companyMoney = jobjD["data"]["basicData"]["regCapital"].ToString();  // 注册资本
                                                                    rela.buildDate = jobjD["data"]["basicData"]["startDate"].ToString();  //成立日期
                                                                    rela.companysprid = jobjD["data"]["basicData"]["openTime"].ToString();  //经营期限
                                                                    rela.companyscope = jobjD["data"]["basicData"]["scope"].ToString();  //经营（业务）范围
                                                                    rela.industry = jobjD["data"]["basicData"]["industry"].ToString();  //所属行业
                                                                    rela.companystatus = jobjD["data"]["basicData"]["openStatus"].ToString(); // 
                                                                    rela.companycode = jobjD["data"]["basicData"]["unifiedCode"].ToString();  // 统一社会信用代码
                                                                    rela.companypeoples = "";  // 
                                                                    rela.companyarea = jobjD["data"]["basicData"]["district"].ToString();   // 
                                                                    rela.companydiscrip = jobjD["data"]["basicData"]["describe"].ToString();  // 
                                                                    rela.companyphoto = jobjD["data"]["basicData"]["telephone"].ToString();
                                                                    rela.companyemail = jobjD["data"]["basicData"]["email"].ToString();
                                                                    string websi = jobjD["data"]["basicData"]["website"].ToString();
                                                                    if (websi.Length >= 100)
                                                                    {
                                                                        websi = websi.Substring(0, 99);
                                                                    }
                                                                    rela.website = websi;
                                                                    _company.Update(rela);
                                                                    Console.WriteLine("成功爬取[" + rela.companyfullname + "]," + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
                                                                    isok = true;
                                                                }
                                                                else
                                                                {
                                                                    Console.WriteLine("可能被屏蔽了，3请查看:" + RedirectUrl + "," + jobjD.ToJsonString());
                                                                    Thread.Sleep(1000);
                                                                }
                                                            }
                                                        }
                                                        catch (Exception ex)
                                                        {
                                                            Console.WriteLine("可能被屏蔽了，4请查看:" + newurl + "," + RedirectUrl + "," + rela.companyname + "," + html + ex.Message);
                                                            Thread.Sleep(1000);
                                                        }
                                                    }
                                                }
                                                else
                                                {
                                                    Console.WriteLine("RedirectUrl:" + RedirectUrl);
                                                    Thread.Sleep(1000);
                                                }
                                            }
                                            else
                                            {
                                                Thread.Sleep(1000);
                                            }
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine("可能被屏蔽了，7请查看" + ex.StackTrace);
                                    Thread.Sleep(1000);
                                }
                            }
                        }
                        uowHandle.Complete();
                    }
                }
                catch (Exception)
                {
                }
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        [RemoteService(false)]
        public HttpItem ReHttpItem(string url)
        {
            bool isIP = Convert.ToBoolean(_appConfiguration.GetConnectionString("IP"));
            //创建Httphelper对象

            //创建Httphelper参数对象

            WebProxy webProxy = new WebProxy();
            webProxy.Address = new Uri(string.Format("{0}:{1}", "http://tps163.kdlapi.com", "15818"));
            webProxy.Credentials = new NetworkCredential("t10853562674897", "4j83ks88"); //用户名：t19186421608282  密码：7a85i5lf

            HttpItem item = new HttpItem()
            {
                URL = url,//URL     必需项    
                Method = "GET",//URL     可选项 默认为Get   
                Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
                Cookie = "BIDUPSID=C272367E3D85F5ECDC4FCB4BD9F6BCF9; PSTM=1598611814; ZX_UNIQ_UID=722a76031a1ef614585bde0da76b80c4; Hm_lvt_baca6fe3dceaf818f5f835b0ae97e4cc=1599791699,1599792332; ZX_HISTORY=%5B%7B%22visittime%22%3A%222020-09-11+10%3A48%3A38%22%2C%22pid%22%3A%22xlTM-TogKuTwZbKDp9AU7PhY0n9kUH7M1Amd%22%7D%5D; Hm_lpvt_baca6fe3dceaf818f5f835b0ae97e4cc=1599792520; BDRCVFR[tFA6N9pQGI3]=mk3SLVN4HKm; delPer=0; PSINO=6; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; ZD_ENTRY=baidu; BAIDUID=1B1B9DA3514D7EC08D4B0F2E3AD9C0F0:FG=1; H_PS_PSSID=32699",
                Host = "aiqicha.baidu.com",
                Referer = url,
                UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36",
                //PostDataType = PostDataType.String,//默认为字符串，同时支持Byte和文件方法
                //PostEncoding = System.Text.Encoding.UTF8,//默认为Default，
                //Postdata = "__VIEWSTATE=%2FwEPDwUKLTQ3MzE1NzE2MWQYAQUeX19Db250cm9sc1JlcXVpcmVQb3N0QmFja0tleV9fFgIFCmlzUmVtZW1iZXIFC2lzQXV0b0xvZ2luBX5spPlbkbdaOOfsiLY2g0mJe7M%3D&__VIEWSTATEGENERATOR=C2EE9ABB&WebUCHead_Special1%24hiddIsLogin=0&WebUCSearchNoAdvSearch1%24lib=&keyword=&user_name=" + user + "&user_password=" + password,//Post要发送的数据
                //ContentType = "application/x-www-form-urlencoded",
                SecurityProtocol = SecurityProtocolType.Tls,
                WebProxy = isIP ? webProxy : null
            };

            item.Header.Add("Accept-Encoding", "gzip, deflate, br");
            item.Header.Add("Accept-Language", "zh-CN, zh; q=0.9");
            item.Header.Add("Connection", "keep-alive");
            item.Header.Add("sec-fetch-mode", "navigate");
            item.Header.Add("sec-fetch-site", "same-origin");
            item.Header.Add("sec-fetch-user", "?1");
            item.Header.Add("Origin", "https://aiqicha.baidu.com");
            item.Header.Add("Upgrade-Insecure-Requests", "1");
            return item;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="companyname"></param>
        /// <returns></returns>
        [UnitOfWork(System.Transactions.IsolationLevel.ReadUncommitted)]
        public string GetcompanyList(string companyname)
        {
            try
            {
                string tttmep = companyname.Replace(" ", "").Replace("(", "（").Replace(")", "）");
                MatchCollection mcttt = Regex.Matches(tttmep, "（.{1,25}?）");  //有问题
                if (mcttt.Count == 1 && tttmep.Substring(tttmep.Length - 1, 1) == "）")
                {
                    tttmep = tttmep.Replace(mcttt[0].Groups[0].Value, "");
                }
                if (mcttt.Count > 1)
                {
                    tttmep = tttmep.Replace(mcttt[mcttt.Count - 1].Groups[0].Value, "");
                }
                if (tttmep.IndexOf("分公司") > 0)
                {
                    tttmep = tttmep.Substring(0, tttmep.IndexOf("分公司") + 3);
                }
                else if (tttmep.IndexOf("分行") > 0)
                {
                    tttmep = tttmep.Substring(0, tttmep.IndexOf("分行") + 2);
                }
                else if (tttmep.IndexOf("支公司") > 0)
                {
                    tttmep = tttmep.Substring(0, tttmep.IndexOf("支公司") + 3);
                }
                else if (tttmep.IndexOf("支行") > 0)
                {
                    tttmep = tttmep.Substring(0, tttmep.IndexOf("支行") + 2);
                }
                else if (tttmep.IndexOf("公司") > 0)
                {
                    tttmep = tttmep.Substring(0, tttmep.IndexOf("公司") + 2);
                }
                bool isok = false;
                Console.WriteLine("爬取开始[" + tttmep + "]," + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));

                while (!isok)
                {
                    try
                    {
                        string value = HttpUtility.UrlEncode(tttmep, Encoding.UTF8).Replace("+", "%20");
                        //string url = $"https://aiqicha.baidu.com/s?q={value}&t=0";
                        string url = $"https://aiqicha.baidu.com/s/l?q={value}&t=&p=1&s=10&o=0&f=%7B%7D";
                        HttpItem item = ReHttpItem(url);
                        HttpHelper http = new HttpHelper();
                        HttpResult result = http.GetHtml(item);
                        string content = result.Html.Replace(" ", "");
                        JObject jobj = JObject.Parse(content);

                        if (jobj["status"].ToString() == "0")
                        {

                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("可能被屏蔽了，7请查看" + ex.StackTrace);
                        Thread.Sleep(1000);
                    }
                }
                return "";
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }



    }
}
